package com.tongtech.knowledge.controller;

import com.tongtech.common.core.web.controller.BaseController;
import com.tongtech.common.core.web.domain.AjaxResult;
import com.tongtech.common.core.web.page.TableDataInfo;
import com.tongtech.common.log.annotation.Log;
import com.tongtech.common.log.enums.BusinessType;
import com.tongtech.common.security.annotation.RequiresPermissions;
import com.tongtech.common.security.utils.SecurityUtils;
import com.tongtech.knowledge.domain.Knowledge;
import com.tongtech.knowledge.service.IKnowledgeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 知识库信息
 *
 * @author xingyc
 */
@RestController
@RequestMapping("/knowledge")
public class KnowledgeController extends BaseController
{
    @Autowired
    private IKnowledgeService knowledgeService;

    /**
     * 获取知识库列表
     */
    @RequiresPermissions("knowledge:info:list")
    @GetMapping("/list")
    public TableDataInfo list(Knowledge knowledge)
    {
        startPage();
        List<Knowledge> list = knowledgeService.selectKnowledgeList(knowledge);
        return getDataTable(list);
    }

    /**
     * 根据知识库编号获取详细信息
     */
    @RequiresPermissions("knowledge:info:query")
    @GetMapping(value = "/{knowledgeId}")
    public AjaxResult getInfo(@PathVariable Long knowledgeId)
    {
        return success(knowledgeService.selectKnowledgeById(knowledgeId));
    }

    /**
     * 新增知识库
     */
    @RequiresPermissions("knowledge:info:add")
    @Log(title = "知识库管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Knowledge knowledge)
    {
       if(Objects.nonNull(knowledge)){
           knowledge.setCreateBy(SecurityUtils.getUsername());
       }
        return toAjax(knowledgeService.insertKnowledge(knowledge));
    }

    /**
     * 修改知识库
     */
    @RequiresPermissions("knowledge:info:edit")
    @Log(title = "知识库管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Knowledge knowledge)
    {
        if(Objects.nonNull(knowledge)){
            knowledge.setUpdateBy(SecurityUtils.getUsername());
        }
        return toAjax(knowledgeService.updateKnowledge(knowledge));
    }

    /**
     * 删除知识库
     */
    @RequiresPermissions("knowledge:info:remove")
    @Log(title = "知识库管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{knowledgeId}")
    public AjaxResult remove(@PathVariable Long knowledgeId)
    {
        return toAjax(knowledgeService.deleteKnowledgeById(knowledgeId));
    }
}
